package com.jinhua.admin.dao.impl;
import com.jinhua.mybatis.util.MybatisPlus;
import com.jinhua.mybatis.util.MybatisPlusBuild;
import com.jinhua.config.IdUtil;
import com.jinhua.admin.dao.UserDao;
import com.jinhua.admin.entity.UserPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
* 用户会员表的dao层
*
*/

@Repository
public class UserDaoImpl implements UserDao {
        private final int PAGE_SIZE = 10;
    @Override
    public int saveUserIfNotExist(UserPo userPo) {
        return userPo.save();
    }
    @Override
    public int modUser(UserPo userPo) {
        return userPo.update(userPo.build().eq(UserPo::getId, userPo.getId()));
    }
    @Override
    public int delUserByIds(Long... ids) {
        UserPo userPo = new UserPo();
        userPo.setDeletedFlag(Boolean.TRUE);
        return userPo.update(userPo.build().in(UserPo::getId, ids));
    }
    @Override
    public UserPo findUserById(Long id) {
        MybatisPlusBuild build = new MybatisPlusBuild(UserPo.class);
        build.eq(UserPo::getDeletedFlag, Boolean.FALSE);
        build.eq(UserPo::getId, id);
        return MybatisPlus.getInstance().findOne( new UserPo(), build);
    }
    @Override
    public List<UserPo> findUserByAll(UserPo userPo, Integer page, Integer size) {
        MybatisPlusBuild build = new MybatisPlusBuild(UserPo.class);
        build.eq(UserPo::getDeletedFlag, Boolean.FALSE);
        if (userPo.getId() == null) {
            if (StringUtils.isNotEmpty(userPo.getPhone())) {
                build.like(UserPo::getPhone,userPo.getPhone());
            }
            if (StringUtils.isNotEmpty(userPo.getAvatar())) {
                build.like(UserPo::getAvatar,userPo.getAvatar());
            }
            if (StringUtils.isNotEmpty(userPo.getOpenId())) {
                build.like(UserPo::getOpenId,userPo.getOpenId());
            }
            if (userPo.getStatus()!=null ) {
                build.eq(UserPo::getStatus,userPo.getStatus());
            }
            if (StringUtils.isNotEmpty(userPo.getUnionId())) {
                build.like(UserPo::getUnionId,userPo.getUnionId());
            }
            if (StringUtils.isNotEmpty(userPo.getNickName())) {
                build.like(UserPo::getNickName,userPo.getNickName());
            }
            if (StringUtils.isNotEmpty(userPo.getPassword())) {
                build.like(UserPo::getPassword,userPo.getPassword());
            }
            if (StringUtils.isNotEmpty(userPo.getUserName())) {
                build.like(UserPo::getUserName,userPo.getUserName());
            }
            if (userPo.getRoomCardsNum()!=null ) {
                build.eq(UserPo::getRoomCardsNum,userPo.getRoomCardsNum());
            }
            if (page == null) {
                page = 1;
            }
            IdUtil.setTotal(build);
            build.page(page, size==null?PAGE_SIZE:size);
        }
        else {
            build.eq(UserPo::getId, userPo.getId());
        }
        return MybatisPlus.getInstance().findAll( new UserPo()    , build);
    }
}
